package com.chima.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import com.chima.dao.AdviceDao;
import com.chima.domain.Advice;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.service.AdviceService;

public class AdviceServiceImp implements AdviceService{
	private  AdviceDao  adviceDao;
	

	public AdviceDao getAdviceDao() {
		return adviceDao;
	}
	public void setAdviceDao(AdviceDao adviceDao) {
		this.adviceDao = adviceDao;
	}
	public PageResponseBean findAdvice(PageRequestBean pageRequestBean) {
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total = adviceDao.findByTotalCount();
		// 获取分页查询条件
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Advice.class);
//		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<Advice> advices= adviceDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(advices);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}
	public void saveAdvice(Advice advice) {
		adviceDao.save(advice);
	}
	public void updateAdvice(Advice advice) {
		adviceDao.update(advice);
	}
	public void deleteAdvice(int id) {
		adviceDao.delete(id);
		
	}

}
